﻿using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace mstdump_Analyzer
{
  
    public static class SettingsIO
    {
        //
        //----------------------------------------------------
        /// <summary>
        /// import new settings and set them to the application
        /// </summary>
        /// <param name="settingsFilePath">path to the settings file</param>
        public static void Import(string settingsFilePath)
        {
            if (!File.Exists(settingsFilePath))
            {
                throw new FileNotFoundException();
            }
            /// get pointer to the application's settings
            var appSettings = Properties.Settings.Default;
            try
            {
                /// read the new settings
                StreamReader sr = new StreamReader(settingsFilePath);
                string xmlContent = sr.ReadToEnd();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlContent);
                /// scan the settings file.
                XmlNodeList nodes = doc.SelectNodes("//setting");
                foreach (XmlNode node in nodes)
                {
                   // get the messages keywords
                   if (node.Attributes["name"].Value.Equals("messagesKeywords"))
                   {
                       Properties.Settings.Default.messagesKeywords.Clear();
                       foreach (XmlNode err in node.FirstChild.FirstChild.SelectNodes("string"))
                       {
                           appSettings.messagesKeywords.Add(err.InnerText);
                       }
                   }
                   else
                   // get the dmesg keywords
                   if (node.Attributes["name"].Value.Equals("dmesgKeywords"))
                   {
                       Properties.Settings.Default.dmesgKeywords.Clear();
                       foreach (XmlNode err in node.FirstChild.FirstChild.SelectNodes("string"))
                       {
                           appSettings.dmesgKeywords.Add(err.InnerText);
                       }
                   }
                   else
                   // get the font
                   if (node.Attributes["name"].Value.Equals("myFont"))
                   {
                       var cvt = new FontConverter();
                       appSettings.myFont = cvt.ConvertFromString(node.SelectNodes("value")[0].InnerText) as Font;
                   }
                   else
                    // get the font
                    if (node.Attributes["name"].Value.Equals("logsIdleThresholdSec"))
                    {
                        appSettings.logsIdleThresholdSec = Int32.Parse(node.SelectNodes("value")[0].InnerText);
                    }
                }
                sr.Close();
                appSettings.Save();
            }
            catch (Exception ex) // Should make this more specific
            {
                // Could not import settings.
                appSettings.Reload(); // from last set saved, not defaults
                throw new Exception(ex.Message);
            }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// save the application's settings to a file.
        /// </summary>
        /// <param name="settingsFilePath">target file path</param>
        public static void Export(string settingsFilePath)
        {
            Properties.Settings.Default.Save();
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            if (settingsFilePath != config.FilePath)
            {
                config.SaveAs(settingsFilePath);
            }
        }
        //
        //----------------------------------------------------
        //
    }
}